package com.kye.express.business.found.subfragment;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.fondesa.recyclerviewdivider.RecyclerViewDivider;
import com.kye.express.R;
import com.kye.express.business.base.BaseAppCompatFragment;
import com.kye.express.databinding.FragmentFoundwelfareBinding;
import com.kye.express.utils.ScreenUtils;
import com.kye.express.view.ToastManager;
import com.kye.model.bean.FoundWelfareAdsResult;
import com.kye.model.bean.found.ResultQueryActivityInfoList;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;

/**
 * 发现-福利
 */
public class FoundWelfareFragment extends BaseAppCompatFragment implements FoundWelfareFragmentContract.FoundWelfareView {

    private static final String TAG = FoundWelfareFragment.class.getSimpleName();
    protected FragmentFoundwelfareBinding mFoundSubBinding;
    protected MultiTypeAdapter mAdapter;
    protected Items mDatas = new Items();
    protected FoundWelfareFragmentContract.FoundWelfarePresenter mPresenter;

    public static FoundWelfareFragment newInstance() {
        FoundWelfareFragment fragment = new FoundWelfareFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_foundwelfare, container, false);
        initViews(view, savedInstanceState);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initEvents(savedInstanceState);
        mPresenter = new FoundWelfareFragmentContract.FoundWelfarePresenter(getActivity());
        mPresenter.bind(this);
    }

    private void initViews(View view, Bundle savedInstanceState) {
        mFoundSubBinding = DataBindingUtil.bind(view);
        mAdapter = new MultiTypeAdapter();
        mAdapter.register(FoundWelfareAdsResult.class, new FoundWelfareItemHeadViewBinder(getActivity()));
        mAdapter.register(ResultQueryActivityInfoList.class, new FoundWelfareItemViewBinder(getActivity()));
        mFoundSubBinding.rvData.setLayoutManager(new LinearLayoutManager(getActivity()));
        RecyclerViewDivider.with(getActivity())
                .color(getResources().getColor(R.color.grey))
                .size(ScreenUtils.dp2px(getActivity(), 1))
                .hideLastDivider()
                .build()
                .addTo(mFoundSubBinding.rvData);
        mFoundSubBinding.rvData.setItemAnimator(new DefaultItemAnimator());
        mAdapter.setItems(mDatas);
        mFoundSubBinding.rvData.setAdapter(mAdapter);
        mFoundSubBinding.refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                mPresenter.loadDatas(false, false);
            }

            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                mPresenter.loadDatas(false, true);
            }

        });
    }

    private void addHeadDatas() {
        mDatas.clear();
        //TODO test datas
        ArrayList<String> iamges = new ArrayList<String>();
        for (int i = 0; i < 4; i++) {
            iamges.add("http://pic4.nipic.com/20091217/3885730_124701000519_2.jpg");
        }
        mDatas.add(new FoundWelfareAdsResult(iamges));
        mAdapter.notifyDataSetChanged();
    }

    /**
     * view page中用来加载数据，页面显示后才加载，避免提前加载
     */
    @Override
    protected void initDataFirstTime(boolean isForceUpdate, @Nullable Bundle savedInstanceState) {
        mPresenter.loadDatas(true, false);
    }

    private void initEvents(Bundle savedInstanceState) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mPresenter.unbind();
    }

    @Override
    public void onComplete(boolean isLoadMore) {
        if (isLoadMore) {
            mFoundSubBinding.refreshLayout.finishLoadmore();
        } else {
            mFoundSubBinding.refreshLayout.finishRefresh();
        }
    }

    @Override
    public void onLoading() {
        mFoundSubBinding.refreshLayout.autoRefresh();
    }

    @Override
    public void onLoaded(List<ResultQueryActivityInfoList> resultDatas, boolean isLoadMore) {
        if (resultDatas != null) {
            if (!isLoadMore) {
                mDatas.clear();
                mDatas.addAll(resultDatas);
                mAdapter.notifyDataSetChanged();
            } else {
                List<ResultQueryActivityInfoList> datas = (List<ResultQueryActivityInfoList>) mAdapter.getItems();
                if (datas != null) {
                    int start = datas.size();
                    datas.addAll(resultDatas);
                    mAdapter.notifyDataSetChanged();
                }
            }
        }
    }

    @Override
    public void onError(String msg, boolean isLoadMore) {
        ToastManager.show(getActivity(), msg, Toast.LENGTH_SHORT);
        if (isLoadMore) {
            mFoundSubBinding.refreshLayout.finishLoadmore(false);
        } else {
            mFoundSubBinding.refreshLayout.finishRefresh(false);
        }
    }
}
